package practice.knowledge;
import java.util.Random;
import java.util.Arrays;

public class Matrix {
    private int rows; // 行数
    private int cols; // 列数

    private double[][] data; // 矩阵数据

    // 构造函数
    public Matrix(int rows, int cols) {
        if (rows <= 0 || cols <= 0) {
            throw new IllegalArgumentException("行数和列数必须大于0");
        }
        this.rows = rows;
        this.cols = cols;
        this.data = new double[rows][cols]; // 初始化一个二维数组，初始时元素都为0.0
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }
   
    public int getRows() {
        return rows;
    }


    public int getCols() {
        return cols;
    }

    // 初始化矩阵的值
    public void initValue() {
        Random random = new Random(); 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                data[i][j] = random.nextDouble() * 10; // 随机生成0到10之间的浮点数
            }
        }
    }

    // 矩阵加法
    public Matrix add(Matrix b) {
        if (this.rows != b.rows || this.cols != b.cols) {
            throw new IllegalArgumentException("矩阵维度不匹配，无法相加");
        }
        Matrix result = new Matrix(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[i][j] = this.data[i][j] + b.data[i][j];
            }
        }
        return result;
    }

    // 矩阵减法
    public Matrix sub(Matrix c) {
        if (this.rows != c.rows || this.cols != c.cols) {
            throw new IllegalArgumentException("矩阵维度不匹配，无法相减");
        }
        Matrix result = new Matrix(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[i][j] = this.data[i][j] - c.data[i][j];
            }
        }
        return result;
    }

    // 判断两个矩阵是否相等
    @Override
    public boolean equals(Object obj) {
        if (this == obj) // 检查是否与同一个对象引用相同
            return true;

        if (!(obj instanceof Matrix)) // 检查obj是否是Matrix类的实例
            return false;

        Matrix other = (Matrix) obj; // 将obj强制转换为Matrix类型，以便访问其属性

        // 比较行数、列数和数据内容是否相等
        return this.rows == other.rows && this.cols == other.cols && Arrays.deepEquals(this.data, other.data); // 使用深度比较数组内容
    }

    // 打印矩阵
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(); // 创建一个 StringBuilder 对象，用于高效构建字符串
        for (double[] row : data) {
            for (double val : row) {
                sb.append(String.format("%.2f ", val)); // 将当前元素格式化为小数点后两位，并添加到 StringBuilder 中
            }
            sb.append("\n"); // 在每一行结束后添加换行符，以便在最终字符串中分行显示
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Matrix A = new Matrix(3, 4);
        A.initValue();
        System.out.print("矩阵 A:\n" + A);

        Matrix B = new Matrix(3, 4);
        B.initValue();
        System.out.print("矩阵 B:\n" + B);

        Matrix C = A.add(B);
        System.out.println("A + B:\n" + C);

        Matrix D = A.sub(B);
        System.out.println("A - B:\n" + D);
    }
}
